package com.jvxb.manage.livable.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jvxb.common.web.RespMsg;
import com.jvxb.manage.livable.service.dbQuery.MysqlQueryService;
import com.jvxb.manage.livable.service.dbQuery.RedisQueryService;
import com.jvxb.manage.livable.vo.MySQLQueryVo;
import com.jvxb.manage.livable.vo.RedisQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author jvxb
 * @since 2022-01-05
 */
@RestController
@RequestMapping("/dataQuery/v1")
public class DataQueryController {

    @Autowired
    private MysqlQueryService mysqlQueryService;
    @Autowired
    private RedisQueryService redisQueryService;

    @RequestMapping(value = "/database", method = RequestMethod.POST)
    public RespMsg getDatabase(@RequestBody MySQLQueryVo mySQLQueryVo) {
        List<String> database = mysqlQueryService.getDatabase(mySQLQueryVo);
        return RespMsg.ok(database);
    }

    @RequestMapping(value = "/table", method = RequestMethod.POST)
    public RespMsg getTable(@RequestBody MySQLQueryVo mySQLQueryVo) {
        List<String> database = mysqlQueryService.getTable(mySQLQueryVo);
        return RespMsg.ok(database);
    }

    @RequestMapping(value = "/showCreateTable", method = RequestMethod.POST)
    public RespMsg showCreateTable(@RequestBody MySQLQueryVo mySQLQueryVo) {
        Assert.hasLength(mySQLQueryVo.getHost(), "参数host不能为空");
        Assert.hasLength(mySQLQueryVo.getPort(), "参数port不能为空");
        Assert.hasLength(mySQLQueryVo.getDbName(), "参数dbName不能为空");
        Assert.hasLength(mySQLQueryVo.getTableName(), "参数tableName不能为空");
        String createTable = mysqlQueryService.showCreateTable(mySQLQueryVo);
        return RespMsg.ok(createTable);
    }

    @RequestMapping(value = "/getAllSql", method = RequestMethod.POST)
    public RespMsg getAllSql(@RequestBody MySQLQueryVo mySQLQueryVo) {
        Assert.hasLength(mySQLQueryVo.getSqls(), "参数sqls不能为空");
        List<String> sqls = mysqlQueryService.getAllSql(mySQLQueryVo.getSqls());
        return RespMsg.ok(sqls);
    }

    @RequestMapping(value = "/formatSql", method = RequestMethod.POST)
    public RespMsg formatSql(@RequestBody MySQLQueryVo mySQLQueryVo) {
        Assert.hasLength(mySQLQueryVo.getSqls(), "参数sqls不能为空");
        String formatSql = mysqlQueryService.formatSql(mySQLQueryVo.getSqls());
        return RespMsg.ok(formatSql);
    }

    @RequestMapping(value = "/sqlCount", method = RequestMethod.POST)
    public RespMsg sqlCount(@RequestBody MySQLQueryVo mySQLQueryVo) {
        Assert.hasLength(mySQLQueryVo.getHost(), "参数host不能为空");
        Assert.hasLength(mySQLQueryVo.getPort(), "参数port不能为空");
        Assert.hasLength(mySQLQueryVo.getDbName(), "参数dbName不能为空");
        Assert.hasLength(mySQLQueryVo.getTableName(), "参数tableName不能为空");
        Assert.hasLength(mySQLQueryVo.getSqls(), "参数sqls不能为空");
        return RespMsg.ok();
    }

    @RequestMapping(value = "/mysql/query", method = RequestMethod.POST)
    public RespMsg mysqlQuery(@RequestBody MySQLQueryVo mySQLQueryVo) {
        Assert.notNull(mySQLQueryVo.getClusterId(), "参数clusterId不能为空");
        Assert.hasLength(mySQLQueryVo.getHost(), "参数host不能为空");
        Assert.hasLength(mySQLQueryVo.getPort(), "参数port不能为空");
        Assert.hasLength(mySQLQueryVo.getDbName(), "参数dbName不能为空");
        Assert.hasLength(mySQLQueryVo.getSqls(), "参数sqls不能为空");
        Map executeResult = mysqlQueryService.mysqlQuery(mySQLQueryVo);
        return RespMsg.ok(executeResult);
    }

    @GetMapping("/mysql/query/record")
    public RespMsg getMysqlQueryRecord(Long clusterId,
                                       @RequestParam(defaultValue = "10") Integer size,
                                       @RequestParam(defaultValue = "1") Integer current) {
        try {
            Assert.notNull(clusterId, "param clusterId must not be null");
            IPage records = new Page<>(current, size);
            mysqlQueryService.getMysqlQueryRecord(clusterId, records);
            return RespMsg.ok(records);
        } catch (Exception e) {
            return RespMsg.error("获取redis查询记录异常：" + e.getMessage());
        }
    }


    @PostMapping("/redis/query")
    public RespMsg redisQuery(@RequestBody RedisQueryVo redisQueryVo) {
        try {
            Long clusterId = redisQueryVo.getClusterId();
            String command = redisQueryVo.getCommand();
            Assert.notNull(clusterId, "param clusterId must not be null");
            Assert.notNull(command, "param command must not be null");
            String result = redisQueryService.query(clusterId, command);
            return RespMsg.ok(result);
        } catch (Exception e) {
            return RespMsg.error("查询异常：" + e.getMessage());
        }
    }

    @GetMapping("/redis/query/record")
    public RespMsg getRedisQueryRecord(Long clusterId,
                                       @RequestParam(defaultValue = "10") Integer size,
                                       @RequestParam(defaultValue = "1") Integer current) {
        try {
            Assert.notNull(clusterId, "param clusterId must not be null");
            IPage records = new Page<>(current, size);
            redisQueryService.getRedisQueryRecord(clusterId, records);
            return RespMsg.ok(records);
        } catch (Exception e) {
            return RespMsg.error("获取redis查询记录异常：" + e.getMessage());
        }
    }

}
